package com.ruoyi.project.order.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.order.commons.OrderConst;
import com.ruoyi.project.order.domain.Order;
import com.ruoyi.project.order.service.IOrderAddService;
import com.ruoyi.project.order.service.IOrderEditService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author jy
 * @version 1.0
 */
@Controller
@RequestMapping("/order")
public class OrderEditController extends BaseController {
    @Autowired
    private IOrderEditService orderEditService;
    @Autowired
    private IOrderAddService orderAddService;

    /**
     * 根据主键查询订单信息
     * @param id
     * @return  视图
     */
    @RequiresPermissions("order:edit")
    @GetMapping("/edit/{id}")
    public String toEdit(@PathVariable("id") Long id, Model model){
        Order order =orderEditService.selectOrderByOrderId(id);
        model.addAttribute("order",order);
        return OrderConst.prefix+"/edit";
    }

    /**
     *根据主键更新
     * @return
     */
    @RequiresPermissions("order:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(Order order){
        if (OrderConst.ORDER_NAME_NOT_UNIQUE.equals(orderAddService.checkOrderNameUnique(order)))
        {
            return error("新增订单'" + order.getOrderName() + "'失败，订单项目名称已存在");
        }
        return toAjax(orderEditService.editOrder(order));
    }

}
